<?php 

class RulesContestManager
{

	private $_db;
	
	public function __construct(PDO $db)
	{
		$this->setDb($db);
	}
	
	public function setDb($db)
	{
		$this->_db = $db;
	}
	
	public function add(Object $rulesContest)
	{
		//On teste le type de RulesContest
		switch(get_class($rulesContest))
		{
			case "RulesContestBasket": //Si c'est un concours sur du basket : on ajoute un rulesContest dans la table rulescontest_basket
				$reqInsert = $this->_db->prepare("INSERT INTO rulescontests_basket SET contest_id= :contest_id, winningTeam= :winningTeam, pointsDifference= :pointsDifference, pointsTotal= :pointsTotal, overtime= :overtime, pointsForWTOK= :pointsForWTOK, pointsForPDOK= :pointsForPDOK, pointsForPTOK= :pointsForPTOK, pointsForOOK= :pointsForOOK");
				$reqInsert->bindValue(":winningTeam", $rulesContest->winningTeam());
				$reqInsert->bindValue(":pointsDifference", $rulesContest->pointsDifference());
				$reqInsert->bindValue(":pointsTotal", $rulesContest->pointsTotal());
				$reqInsert->bindValue(":overtime", $rulesContest->overtime());
				$reqInsert->bindValue(":pointsForWTOK", $rulesContest->pointsForWTOK());
				$reqInsert->bindValue(":pointsForPDOK", $rulesContest->pointsForPDOK());
				$reqInsert->bindValue(":pointsForPTOK", $rulesContest->pointsForPTOK());
				$reqInsert->bindValue(":pointsForOOK", $rulesContest->pointsForOOK());
				break;
			default: //Si c'est une classe qui n'est pas "connue" : on ne fait rien
				return;
		}
		$reqInsert->bindValue(":contest_id", $rulesContest->contest_id());
		$reqInsert->execute();
		$idRulesContest = $this->_db->lastInsertId();
		$rulesContest->hydrate(array(
			'id' => $idRulesContest
		));
	}
	public function delete(Object $rulesContest)
	{
		//On teste le type de RulesContest
		switch(get_class($rulesContest))
		{
			case "RulesContestBasket": //Si c'est un concours sur du basket : on supprime le rulesContest dans la table rulescontest_basket
				$reqDelete = $this->_db->prepare("DELETE FROM rulescontest_basket WHERE id= :id");
				break;
			default: //Si c'est une classe qui n'est pas "connue" : on ne fait rien
				return;
		}
		$reqDelete->bindValue(":id", $rulesContest->id());
		$reqDelete->execute();
	}
	public function update(RulesContest $rulesContest)
	{
		//On teste le type de RulesContest
		switch(get_class($rulesContest))
		{
			case "RulesContestBasket": //Si c'est un concours sur du basket : on met à jour le rulesContest dans la table rulescontest_basket
				$reqUpdate = $this->_db->prepare("UPDATE rulescontest_basket SET contest_id= :contest_id, winningTeam= :winningTeam, pointsDifference= :pointsDifference, pointsTotal= :pointsTotal, overtime= :overtime, pointsForWTOK= :pointsForWTOK, pointsForPDOK= :pointsForPDOK, pointsForPTOK= :pointsForPTOK, pointsForOOK= :pointsForOOK WHERE id= :id");
				$reqUpdate->bindValue(":winningTeam", $rulesContest->winningTeam(), PDO::PARAM_BOOL);
				$reqUpdate->bindValue(":pointsDifference", $rulesContest->pointsDifference(), PDO::PARAM_INT);
				$reqUpdate->bindValue(":pointsTotal", $rulesContest->pointsTotal(), PDO::PARAM_INT);
				$reqUpdate->bindValue(":overtime", $rulesContest->overtime(), PDO::PARAM_BOOL);
				$reqUpdate->bindValue(":pointsForWTOK", $rulesContest->pointsForWTOK(), PDO::PARAM_INT);
				$reqUpdate->bindValue(":pointsForPDOK", $rulesContest->pointsForPDOK(), PDO::PARAM_INT);
				$reqUpdate->bindValue(":pointsForPTOK", $rulesContest->pointsForPTOK(), PDO::PARAM_INT);
				$reqUpdate->bindValue(":pointsForOOK", $rulesContest->pointsForOOK(), PDO::PARAM_INT);
			default: //Si c'est une classe qui n'est pas "connue" : on ne fait rien
				return;
		}
		$reqUpdate->bindValue(":id", $rulesContest->id(), PDO::PARAM_INT);
		$reqUpdate->bindValue(":contest_id", $rulesContest->contest_id(), PDO::PARAM_INT);
		$reqUpdate->execute();
	}
	
	public function get(Sport $sport, Contest $contest)
	{
		//Fonction qui va chercher un rulesContest dans la table contests à partir du critère de recherche contest
		switch($sport->id())
		{
			case 1://C'est du basket
				$reqSelect = $this->_db->prepare("SELECT * FROM rulescontest_basket WHERE contest_id= :contest_id");
				break;
			default:
				return null;
		}
		$reqSelect->bindValue(":contest_id", $contest->id());
		$reqSelect->execute();
		$rulesContest = $reqSelect->fetch(PDO::FETCH_ASSOC);
		if(is_array($rulesContest))
		{
			switch($sport->id())
			{
				case 1://C'est du basket
					return new RulesContestBasket($rulesContest);
				default:
					return null;
			}
		}
		return null;
	}
	
}